package com.jic.filecenter.service.upload;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.ObjectMetadata;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;

@Component
@Slf4j
public class AliOssSDKUploadFile {

    @Value("${aliyun.oss.endpoint}")
    String endpoint;

    @Value("${aliyun.oss.accessKeyId}")
    String accessKeyId;

    @Value("${aliyun.oss.accessKeySecret}")
    String accessKeySecret;

    @Value("${aliyun.oss.bucket}")
    String bucketName;


    @Async
    public void aliOssUpload(String key, Long fileSize, String contentType, String name , InputStream stream) throws IOException {
        ObjectMetadata meta = new ObjectMetadata();
        meta.setContentLength(fileSize);
        meta.setContentType(contentType);
        meta.setObjectAcl(CannedAccessControlList.PublicRead);
        String fileName = name;
        meta.setContentDisposition("attachment;filename=\""+fileName+"\"");
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        ossClient.putObject(bucketName, key, stream, meta);
        ossClient.shutdown();
        log.info("async upload file success");
    }
}
